package com.ruoyi.tzzl.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.tzzl.domain.WzDynamic;
import com.ruoyi.tzzl.domain.WzGiveLike;
import com.ruoyi.tzzl.domain.vo.QueryVo;
import com.ruoyi.tzzl.mapper.WzGiveLikeMapper;
import com.ruoyi.tzzl.service.IWzDynamicService;
import com.ruoyi.tzzl.service.IWzGiveLikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 用户动态点赞Service业务层处理
 * @author by caoyong
 * @date 2022/11/19
 **/
@Service
public class WzGiveLikeServiceImpl extends ServiceImpl<WzGiveLikeMapper, WzGiveLike> implements IWzGiveLikeService {

  @Autowired
  private IWzDynamicService wzDynamicService;


    /**
     * 点赞
     *
     * @param queryVo
     */
    @Override
    public boolean giveAlike(QueryVo queryVo) {
        LambdaQueryWrapper<WzGiveLike> lqw =new LambdaQueryWrapper<>();
        //先判断是否已经点赞
        lqw.eq(WzGiveLike::getId,queryVo.getId());
        lqw.eq(WzGiveLike::getCid,queryVo.getWatch_id());
        lqw.isNotNull(WzGiveLike::getDelFlag);
        WzGiveLike wzGiveLike = this.getOne(lqw);
        if (ObjectUtils.isEmpty(wzGiveLike)){
            WzDynamic wzDynamic = wzDynamicService.getById(queryVo.getId());
            wzDynamic.setLikenums(wzDynamic.getLikenums()+1);
            WzGiveLike wzGiveLike1 =new WzGiveLike();
            wzGiveLike1.setDid(wzDynamic.getId()); //动态表id
            wzGiveLike1.setWid(wzDynamic.getWid()); //发布动态用户id
            wzGiveLike1.setCid(queryVo.getWatch_id()); //点赞用户id
            wzGiveLike1.setCreateTime(new Date()); //创建时间
            this.save(wzGiveLike1);
            return wzDynamicService.updateById(wzDynamic);
        }else{
            wzGiveLike.setDelFlag("0");
            WzDynamic wzDynamic = wzDynamicService.getById(queryVo.getId());
            wzDynamic.setLikenums(wzDynamic.getLikenums()+1);
            wzDynamicService.updateById(wzDynamic);
           return  this.updateById(wzGiveLike);
        }

    }

    @Override
    public boolean cancel(QueryVo queryVo) {
        LambdaQueryWrapper<WzGiveLike> lqw =new LambdaQueryWrapper<>();
        lqw.eq(WzGiveLike::getDid,queryVo.getId());
        lqw.eq(WzGiveLike::getCid,queryVo.getWatch_id());
        WzGiveLike wzGiveLike = this.getOne(lqw);
        //点赞数量减一
       WzDynamic wzDynamic = wzDynamicService.getById(queryVo.getId());
       wzDynamic.setLikenums(wzDynamic.getLikenums() - 1);
       wzDynamicService.updateById(wzDynamic);

       if (ObjectUtils.isNotEmpty(wzGiveLike)){
          return this.removeById(wzGiveLike.getId());
       }
        return false;
    }

    /**
     * 判断是否点了赞
     *
     * @param queryVo
     */
    @Override
    public String judge(Long did, Long cid) {
        LambdaQueryWrapper<WzGiveLike> lqw =new LambdaQueryWrapper<>();
        lqw.eq(WzGiveLike::getCid,cid);
        lqw.eq(WzGiveLike::getDid,did);
        WzGiveLike wzGiveLike = this.getOne(lqw);
        if (ObjectUtils.isNotEmpty(wzGiveLike)){
            return "1";
        }else{
            return "0";
        }
    }
}
